package com.application.meizhi.ui.fragment.jiandan;

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.application.meizhi.R;
import com.application.meizhi.adapter.DouBanListAdapter;
import com.application.meizhi.adapter.ViewPagerAdapter;
import com.application.meizhi.base.MeiziConfig;
import com.application.meizhi.persenter.jiandan.JianDanPresenter;
import com.application.meizhi.ui.activity.douban.DouBanDetailActivity;
import com.application.meizhi.ui.fragment.BaseFragment;
import com.application.meizhi.ui.iView.jiandan.IJianDanView;
import com.application.meizhi.ui.widget.LoadingFooter;
import com.application.meizhi.uitl.RecyclerViewStateUtils;
import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.orhanobut.logger.Logger;
import com.zeng.headandfooterlibrary.EndlessRecyclerOnScrollListener;
import com.zeng.headandfooterlibrary.HeaderAndFooterRecyclerViewAdapter;

import java.util.ArrayList;

import butterknife.BindView;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import in.srain.cube.views.ptr.header.MaterialHeader;
import in.srain.cube.views.ptr.util.PtrLocalDisplay;

/**
 * Created by zeng on 2016/10/15.
 */

public class JianDanFragment extends BaseFragment<JianDanPresenter> implements IJianDanView {

    @BindView(R.id.ptrFrame)
    PtrFrameLayout mFrameLayout;

    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;

    private Integer page = 1;

    private int mCurrentCount = 0;

    @Override
    public void initView() {

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_list;
    }

    @Override
    protected void initPresenter() {
        presenter = new JianDanPresenter(getActivity(), this);
        presenter.init();
    }


    private void initRecycler() {


        // mAdapter = new DouBanListAdapter(getActivity());

//        HeaderAndFooterRecyclerViewAdapter adapter = new HeaderAndFooterRecyclerViewAdapter(mAdapter);
//        mRecyclerView.addOnScrollListener(mScrollListener);
//        mRecyclerView.setAdapter(adapter);
//        mRecyclerView.setHasFixedSize(true);
//        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
//        mRecyclerView.setLayoutManager(manager);


    }

    private void initRefreshLayout() {

        MaterialHeader header = new MaterialHeader(getActivity());
        header.setPadding(0, PtrLocalDisplay.dp2px(20), 0, PtrLocalDisplay.dp2px(20));
        header.setLayoutParams(new PtrFrameLayout.LayoutParams(-1, -2));
        mFrameLayout.setDurationToCloseHeader(1000);
        mFrameLayout.setHeaderView(header);
        mFrameLayout.addPtrUIHandler(header);
        mFrameLayout.setPinContent(true);//设置为true时content的内容位置将不会改变

        mFrameLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                page = 1;
            }
        });

    }

    private EndlessRecyclerOnScrollListener mScrollListener = new EndlessRecyclerOnScrollListener() {

        @Override
        public void onLoadNextPage(View view) {
            super.onLoadNextPage(view);
            LoadingFooter.State state = RecyclerViewStateUtils.getFooterViewState(mRecyclerView);
            if (state == LoadingFooter.State.Loading) {
                Logger.d("the state is Loading, just wait..");
                return;
            }
            if (mCurrentCount < MeiziConfig.PAGE_SIZE) {
                //the end
                RecyclerViewStateUtils.setFooterViewState(getActivity(), mRecyclerView, MeiziConfig.PAGE_SIZE, LoadingFooter.State.TheEnd, null);
            } else {
                // loading more
                RecyclerViewStateUtils.setFooterViewState(getActivity(), mRecyclerView, MeiziConfig.PAGE_SIZE, LoadingFooter.State.Loading, null);

            }
        }
    };
}
